/*	Rais_MixlyEasy:
	this file is a part of Rais_MixlyLibs.
	
	Created: 2020.4.1
	by Raiscies.
	
*/

#ifndef RAIS_MIXLYEASY_H
#define RAIS_MIXLYEASY_H

#include <Arduino.h>

//Macro functions:
//drawing image to Rais::MatrixLED or any other matrix type that overloaded operator()(x, y, new_state)
template <class INST, typename IMG>	//INST is instance of matrix type, IMG is image 's type
void drawPgmImgToMat(INST& inst, IMG& img, int16_t x, int16_t y, int16_t end_x, int16_t end_y, int16_t shft_x, int16_t shft_y){
	uint8_t bfr_img[8];
	memcpy_P(&bfr_img, &img, 8);
	for(int8_t img_x = 0; img_x < 8; img_x++){
		if(x + shft_x + img_x > end_x) continue;
		for(int8_t img_y = 0; img_y < 8; img_y++){
			if(y + 7  + shft_y - img_y > end_y) continue;
			inst(x + shft_x + img_x, y + 7 + shft_y - img_y, (bfr_img[img_y] >> img_x) & 1);
		}
	}
}


//Rais_Graph  - Orthogonal Graph: getting turning direction
//正交图: 由目前朝向和目标朝向返回转向的动作
constexpr inline byte get_turning_dir(byte fasing, byte abs_tgt_dir){
	//add 4 to avoid overflow:
	return 4 + abs_tgt_dir - fasing - (abs_tgt_dir < fasing ? 0 : 4);
}


//Digital pins:
//为什么要定义这个呢? 当然是担心有些小可爱这么写啦~
enum: uint8_t{
	D0 = 0,
	D1, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11, D12, D13,
	//Arduino Mega2560 have 54 digital pins...(FUCK
	//本来想删掉的, 但是写都写了...
	D14, D15, D16, D17, D18, D19, D20, D21, D22, D23, D24, D25, D26,
	D27, D28, D29, D30, D31, D32, D33, D34, D35, D36, D37, D38, D39,
	D40, D41, D42, D43, D44, D45, D46, D47, D48, D49, D50, D51, D52, D53
	
};

 
//-----------------Desperated------------------//

//copy mixly built-in PROGNEM type matrix image to Rais_MatrixLED instance:
#define drawImg2RMat(inst, img, x, y, end_x, end_y) do{                        \
    uint8_t bfr_img[8];                                                        \
    memcpy_P(&bfr_img, &img, 8);                                               \
    for(uint8_t img__x = 0; img__x < 8; img__x++){                             \
        if(x + img__x > end_x) continue;                                       \
        for(uint8_t img__y = 0; img__y < 8; img__y++){                         \
            if(y + 7 - img__y > end_y) continue;                               \
            inst(x + img__x, y + 7 - img__y, (bfr_img[img__y] >> img__x) & 1); \
        }                                                                      \
    }                                                                          \
}while(0)

#endif